/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketManager.ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.xml.registry.infomodel.User;
import ticketManager.model.ManagerUser;
import ticketManager.model.UserGroup;

/**
 *
 * @author shengruoya
 */
@Stateless
public class UserDAO {

    @PersistenceContext
    EntityManager em;

    /**
     * Returns user information, if user login and password are correct.
     *
     * @param userName
     * @param password
     * @return a user, or null if login and password don't match.
     */
    public ManagerUser dologin(String userName, String password) {
        List<ManagerUser> l = findUserByLogin(userName);
        // Special rule for user admin on fist connection (password should be changed afterwards).
        if (userName.equals("admin") && l.isEmpty() && password.equals("admin")) {
            ManagerUser admin = new ManagerUser("admin","admin");
            admin.setUserGroup(UserGroup.ADMIN);
            // admin.setSignature("");
            em.persist(admin);
            return admin;
        }
        if (l.size() == 1 && password.equals(l.get(0).getPassword())) {
            return l.get(0);
        } else {
            return null;
        }
    }

    public void createUser(String userName, String password, UserGroup userGroup) throws UserAlreadyExist {
        /* if(login == null || "".equals(login) || 
         password == null ||"".equals(password)){
         throw new UserEmptyField();
         }*/
        if (findUserByLogin(userName).isEmpty()) {
            ManagerUser u = new ManagerUser(userName,password, userGroup);
            // u.setSignature(signature);
            if(userName.equals("manager")){
                u.setUserGroup(UserGroup.MANAGER);
            }
            em.persist(u);

        } else {
            throw new UserAlreadyExist();
        }

    }

    public List<ManagerUser> getListUsers() {
        List<ManagerUser> Ls = em.createQuery("select u from ManagerUser u ").getResultList();
        return Ls;

    }

    private List<ManagerUser> findUserByLogin(String userName) {
        List<ManagerUser> l = em.
                createQuery("select u from ManagerUser u where u.userName = :userName").
                setParameter("userName", userName).getResultList();
        return l;
    }

    public List<ManagerUser> getAllUsers() {
        List<ManagerUser> allUsers = em.createQuery("select u from ManagerUser u where u.userGroup=:u").setParameter("u", UserGroup.USER)
                .getResultList();
        return allUsers;
    }

    public List<ManagerUser> getUsersExcept(Long id) {
        List<ManagerUser> users = em.createQuery("select u from ManagerUser u where u.id!=:id").setParameter("id", id).getResultList();
        return users;
    }
    public ManagerUser getUserById(Long id){
        ManagerUser m=(ManagerUser)em.createQuery("select m from ManagerUser m where m.id=:id")
                .setParameter("id", id).getSingleResult();
        return m;
    }
    public ManagerUser getUserByUsername(String username){
        ManagerUser m=(ManagerUser)em.createQuery("select m from ManagerUser m where m.userName=:username").setParameter("username", username)
                .getSingleResult();
        return m;
    }
    
    public void removeUser(ManagerUser u)
    {
        em.remove(u);
    }

    public void createUserByLCY(String username,String password,UserGroup usergroup){
        ManagerUser u=new ManagerUser(username,password);
        u.setUserGroup(usergroup);
        em.persist(u);
    }

    public void updateName(Long id,String name){
      ManagerUser m=getUserById(id);
      m.setUserName(name);
    }
    public void updatePassword(Long id,String password){
        ManagerUser m=getUserById(id);
        m.setPassword(password);
    }
                      
}
